import Vue from 'vue';
// import axios from 'axios';
import VueI18n from 'vue-i18n';
import components from '@/components';
import App from './App';
import api from './api';
import store from './store';
import router from './router';
import tools from '@/utils/tools';
import filters from '@/utils/filters';
import jsBridge from '@/utils/jsBridge';
import plugins from './plugins';

import 'lib-flexible/flexible';
// 全局引入index.css
import './assets/style/index.less'; // 过滤器
import VConsole from 'vconsole';

if (process.env.NODE_ENV !== 'production') {
  new VConsole();
}

// 同步加载多语言文件
const files = require.context('@/utils/languages', false, /\.js$/);
const languages = {};

files.keys().forEach(key => {
  languages[key.replace(/\.js$/, '').substring(2)] = files(key).default;
});

// 注册所有插件
Vue.use(plugins);

Vue.prototype.$API = api;
Vue.prototype.$tools = tools;
Vue.config.productionTip = false;
Vue.prototype.$jsBridge = jsBridge;
Vue.prototype.$SERVER_URL = process.env.VUE_APP_SERVER_URL;
Vue.prototype.$WEB_URL = process.env.VUE_APP_SERVER_WEB_URL;
// 注册EventBus事件对象
const EventBus = new Vue();
window.EventBus = EventBus;
Object.defineProperties(Vue.prototype, {
  $BUS: {
    get: function () {
      return EventBus;
    }
  }
});

// 注册全局组件
Object.keys(components).forEach(key => {
  const name = components[key].name;
  Vue.component(name, components[key]);
});

Object.keys(filters).forEach(k => Vue.filter(k, filters[k]));

// 多语言
const i18n = new VueI18n({
  locale: 'zh', // 设置地区
  messages: languages, // 设置地区信息
});

new Vue({
  i18n,
  router,
  store,
  render: h => h(App)
}).$mount('#app');
